/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pacificlms.MainPanel;

import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import pacificlms.LoggedMember;
import pacificlms.Verifiable.Database.*;
import pacificlms.Verifiable.VerifiableObject;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import pacificlms.Error.ErrorHandle;
import pacificlms.MasterConnection;

/**
 * Displays the fines (if any exists associated with this member)
 * @author Brian Hume
 */
public class InformationMembershipFines extends CentralUIPanel {

    /**
     * Creates new form InformationMembershipFines
     */
    public InformationMembershipFines(VerifiableObject VO) {
        super(VO);
        initComponents();
        Create((Member)VO);
    }
    
    Member CurrentMember;
    ArrayList<CheckOut> FineList;
    ArrayList<CheckOut> UnpaidList;
    double TotalFine = 0;
    
    public String TabName()
    {
        return "Member Fines";
    }
    
    /**
     * Sets the controls to enable
     * Fills all of the fields on the panel
     * @param Memb 
     */
    void Create(Member Memb)
    {
        try
        {
            boolean Librarian = LoggedMember.getInstance().IsLibrarian();
            
            btnPayFines.setEnabled(Librarian);
            
            TotalFine = 0;
            
            CurrentMember = Memb;
            ArrayList<VerifiableObject> ALVO = CurrentMember.GetRelatedData(CheckOut.class, 1000);
            FineList = new ArrayList();
            UnpaidList = new ArrayList();
            
            for(VerifiableObject VO: ALVO)
            {
                CheckOut CO = (CheckOut)VO;
                
                double LateFineNum = VerifiableObject.GetNumber(CO.LateFine(null));
                double CondFineNum = VerifiableObject.GetNumber(CO.CopyConditionFine(null));
                double PaymentNum = VerifiableObject.GetNumber(CO.Payment(null));
                
                if (LateFineNum != 0 || CondFineNum != 0)
                {
                    FineList.add(CO);
                    if (LateFineNum + CondFineNum > PaymentNum)
                    {
                        TotalFine = TotalFine + LateFineNum + CondFineNum - PaymentNum;
                        
                        UnpaidList.add(CO);
                    }
                }
            }
            
            txtLibraryCard.setText(Memb.LibraryCardNumber(null));
            
            DefaultListModel DLM = new DefaultListModel();
            for(CheckOut CO: FineList)
            {
                DLM.addElement(CO);
            }
            lstFines.setModel(DLM);
            
            DLM = new DefaultListModel();
            for(CheckOut CO: UnpaidList)
            {
                DLM.addElement(CO);
            }
            lstUnpaid.setModel(DLM);
            
            String displayNumber =  String.format("%.2f", TotalFine);
            txtTotalFine.setText(displayNumber);
            txtPayment.setText(displayNumber);
        }
        catch(Exception Ex)
        {
            System.out.println(Ex.toString());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel11 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        txtLibraryCard = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        btnPayFines = new javax.swing.JButton();
        btnClose = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstFines = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstUnpaid = new javax.swing.JList();
        txtPayment = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtTotalFine = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();

        setPreferredSize(new java.awt.Dimension(615, 535));

        jLabel11.setFont(new java.awt.Font("Tahoma", 0, 22)); // NOI18N
        jLabel11.setText("Member Fine Information");

        jLabel1.setText("Library Card #");

        txtLibraryCard.setEditable(false);

        jLabel3.setText("Fines History");

        jLabel4.setText("Unpaid Fines");

        jLabel5.setText("Payment");

        btnPayFines.setText("Pay Fines");
        btnPayFines.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPayFinesjButton8ActionPerformed(evt);
            }
        });

        btnClose.setText("Close");
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClosejButton9ActionPerformed(evt);
            }
        });

        jScrollPane1.setViewportView(lstFines);

        jScrollPane2.setViewportView(lstUnpaid);

        jLabel6.setText("$");

        jLabel7.setText("Total Fines");

        txtTotalFine.setEditable(false);

        jLabel8.setText("$");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4)
                .addComponent(txtLibraryCard, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 475, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 475, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4)
                .addComponent(txtPayment, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4)
                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(219, 219, 219)
                .addComponent(btnPayFines, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(485, 485, 485)
                .addComponent(btnClose, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4)
                .addComponent(txtTotalFine, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel11)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel1))
                    .addComponent(txtLibraryCard, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel7))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtTotalFine, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel8)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addComponent(jLabel5))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(txtPayment, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addComponent(jLabel6))
                    .addComponent(btnPayFines))
                .addGap(18, 18, 18)
                .addComponent(btnClose))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnPayFinesjButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPayFinesjButton8ActionPerformed
        long PaidTotal = Math.round(Double.valueOf(txtPayment.getText()) * 100.0);
        
        if (TotalFine * 100.0 < PaidTotal)
        {
            JOptionPane.showMessageDialog(null, "You cannot pay more than the total fine, try again.");
            return;
        }
        
        
        for(CheckOut CO: UnpaidList)
        {
            long COPrevPaid = Math.round(VerifiableObject.GetNumber(CO.Payment(null)) * 100.0);
            long COFineValue = Math.round(VerifiableObject.GetNumber(CO.LateFine(null)) * 100.0) + Math.round(VerifiableObject.GetNumber(CO.CopyConditionFine(null)) * 100.0) - COPrevPaid;
            
            long COPaidValue = 0;
            
            if (PaidTotal < COFineValue)
            {
                COPaidValue = PaidTotal + COPrevPaid;
                PaidTotal = 0;
            }
            else
            {
                COPaidValue = COPrevPaid + COFineValue;
                PaidTotal -= COFineValue;
            }
            
            double NewValue = (double)COPaidValue / 100.0;
            String PaidString = String.format("%.2f", NewValue);
            
            try
            {
                CO.Payment(String.valueOf(PaidString));
                if (!CO.Verify())
                    return;
                String SQL = CO.UpdateRecrodSQL();
                Connection Conn = MasterConnection.getInstance();
                Conn.createStatement().execute(SQL);
            }
            catch(SQLException Ex)
            {
                ErrorHandle.Display("Database Error", "Error occured with database,\ntry closing the program and running again or contacting an administrator\n" + Ex.toString(), Ex, true);
                return;
            }
            
            if (PaidTotal == 0)
            {
                break;
            }
        }
        
        Create(CurrentMember);
        
        JOptionPane.showMessageDialog(null, "Thank you for the payment.");
    }//GEN-LAST:event_btnPayFinesjButton8ActionPerformed

    private void btnClosejButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClosejButton9ActionPerformed
        Close();
    }//GEN-LAST:event_btnClosejButton9ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnPayFines;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList lstFines;
    private javax.swing.JList lstUnpaid;
    private javax.swing.JTextField txtLibraryCard;
    private javax.swing.JTextField txtPayment;
    private javax.swing.JTextField txtTotalFine;
    // End of variables declaration//GEN-END:variables
}
